package com.framework.modules.job.task;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowActiv;
import com.framework.entity.sys.SysDept;
import com.framework.entity.ter.TerTerminalDue;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.service.DeptService;
import com.framework.service.FlowActivService;
import com.framework.service.TerTerminalDueService;
import com.framework.service.TerminalInfoService;
import com.framework.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 返现期限处理任务
 * @author wq
 */
@Component("terActivHandleTask")
@Slf4j
public class TerActivHandleTask {
    @Autowired
    private TerTerminalDueService terTerminalDueService;

    @Autowired
    private TerminalInfoService terminalInfoService;
    @Autowired
    private FlowActivService flowActivService;

    @Autowired
    private DeptService deptService;

    /**
     * 处理机器返现期限
     */
    public void handleActivDueBoot(){

        List<SysDept> list = deptService.selectList(new EntityWrapper<SysDept>());

        for (int i = 0; i < list.size(); i++) {
            SysDept sysDept =  list.get(i);
            if(sysDept.getDeptId().equals(8893226L)){
                handleActivDueKaola(sysDept);
            }
        }
    }

    private void handleActivDueKaola(SysDept sysDept) {
        log.info("-------处理"+sysDept.getName()+"机器返现开始--------");
        List<TerTerminalDue> list = terTerminalDueService.selectList(new EntityWrapper<TerTerminalDue>().eq("dept_id", sysDept.getDeptId()));
        list.stream()
                .filter(e->{
                    TerTerminalInfo info = terminalInfoService.queryTerminalBySn(e.getSn());
                    if(info!=null){
                        FlowActiv flowactiv = flowActivService.queryActivBySn(e.getSn());
                        return flowactiv==null && info.getIsActivation()!=2;
                    }else {
                        return false;
                    }
                }).forEach(e->{
                    Date dueDate=e.getDueDate();
                    Date today = DateUtils.stringToDate(DateUtils.format(new Date()), DateUtils.DATE_PATTERN);
                    if(dueDate.compareTo(today)==0){
                        TerTerminalInfo info = terminalInfoService.queryTerminalBySn(e.getSn());
                        info.setReapAmount(e.getAfterCash());
                        terminalInfoService.updateById(info);
                        log.info("-------处理了sn:"+info.getSn()+"的返现金额为:"+e.getAfterCash()+"----------");
                    }
        });

        log.info("-------处理"+sysDept.getName()+"机器返现结束--------");
    }
}
